home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 2: CDPD 1 / Almathera Ten on Ten - Disc 2: CDPD 1.iso / pd / 101-125 / 105 / pere-et-fils / pere-et-fils.doc < prev    next >
Text File  |  1995-03-13  |  4KB  |  118 lines

  1. /**************************************************************************/
  2.                            PERE ET FILS
  3.                          ( Father & Sons )
  4.              An example of creating REENTRANT processes
  5.                         by Jean-Michel Forgeas
  6.                             June 1987
  7. /**************************************************************************/
  8. IMPORTANT:
  9. ----------
  10.     This files are placed into Public Domain by the author.
  11.     You can freely distribute them for non-commercial purposes
  12.     on condition that they all stay together:
  13.  
  14.         proc.h              include file (message structure)
  15.         Pere.c              'father' source
  16.         Fils.c              'child'  source
  17.         StartProc.asm       startup source
  18.         Pere                executable
  19.         Fils                executable
  20.         StartProc.obj       object file to link with
  21.         pere-et-fils.doc    this file
  22.  
  23. /* ----------------------------------------------------------------------- */
  24.  
  25.  
  26.     To try it out:
  27.     --------------
  28.         Set your current directory to pere-et-fils drawer.
  29.         Then type Pere Fils [n]  ( n - the number of processes you want,
  30.                                      try 4 first time).
  31.  
  32.     To stop them all:
  33.     -----------------
  34.         Write 'logoff' into every window.
  35.         'Pere' stops itself after the last 'fils'.
  36.  
  37.  
  38.     HOW IT WORKS:
  39.     -------------
  40.         Father loads ones the child code into memory and then starts as many
  41.         processes as you want over it. The code is as a pure body,
  42.         the process is the spirit to bring this body to life.
  43.  
  44.         Father opens 2 MsgPorts. The first one is used for children
  45.         initilisation and to recieve their answers ( Ok or error codes).
  46.         Then, father changes the ReplyPort in the message structure
  47.         to his second port so to dialog with child process through father's
  48.         second port.
  49.         Father process then can start another child using the first
  50.         MsgPort.
  51.  
  52.         Every child detects the initialisation message coming to his port
  53.         and creates an additional port so not to use the one in the
  54.         Process structure. Before answering 'Ok' to the father, he places
  55.         the address of his new port in the message so that father will know
  56.         to where send his messages.
  57.  
  58.         As every child has its own message, every message contains the
  59.         address of the MsgPort of his child.
  60.  
  61.         Children behave as a terminals and the code that make them work
  62.         is in the father.
  63.  
  64.         So you can create as many child processes as you wish all using
  65.         the same code that exists only ONES in memory.
  66.         Of course, each child's DATA should be different.
  67.         Use AllocMem to reserve space for child's static variables. You can
  68.         use automatic variables since they are placed onto stack but
  69.         static and global variables should be used with great care since
  70.         all chilren will read and write to them all at the same time.
  71.  
  72.  
  73.  
  74.     StartProc.obj:
  75.     --------------
  76.         It is modified astartup.obj to allow for reentrant code. It is
  77.         possible not to use startup at all and kill processes with RemTask(),
  78.         but it's better to use startup for 2 reasons:
  79.  
  80.             - cause exit() function is very handy;
  81.             - RemTask doesn't return the memory (AllocMem) and other
  82.               resources used by the task it is going to remove;
  83.  
  84.  
  85.     STATIC & GLOBAL variables:
  86.     --------------------------
  87.         The easy way to use static and global variables in your reentrant
  88.         code is to define one structure that contains all your variables
  89.         and then call AllocMem with image of this structure. Then you
  90.         would pass an address of this structure through all of your
  91.         function calls.
  92.  
  93.  
  94.  
  95.     COMPILE INSTRUCTIONS:
  96.     ---------------------
  97.         This example MUST be compiled with Lattice C (3.10 or 3.03).
  98.  
  99.                 LC1 -iINCLUDE: Pere
  100.                 LC2 -v Pere
  101.                 BLink FROM LIB:AStartup.obj+Pere.o TO Pere
  102.                       LIBRARY LIB:Amiga.lib+LC.lib VERBOSE NODEBUG
  103.  
  104.                 LC1 -iINCLUDE: Fils
  105.                 LC2 -v Fils
  106.                 BLink FROM LIB:StartProc.obj+Fils.o TO Fils
  107.                 LIBRARY LIB:Amiga.lib VERBOSE NODEBUG
  108.  
  109.  
  110.  
  111.     CONTACT:
  112.     --------
  113.                 J-M. Forgeas,
  114.                 21 rue Voltaire,
  115.                 92300 Levallois Perret,
  116.                 FRANCE
  117.  
  118.